package hr.koshnica.android.ui.social;

import java.util.ArrayList;

import hr.koshnica.android.R;
import hr.koshnica.android.ui.MainActivityV2;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.astuetz.viewpagertabs.ViewPagerTabProvider;
import com.astuetz.viewpagertabs.ViewPagerTabs;

public class SlidingTabsPagerSocial extends SherlockFragmentActivity{

	private static final int MENU_FILTER = 100;
	private static final int MENU_SORT = 200;
	private static final int MENU_SEARCH = 300;
	
	private ViewPager mViewPager;
	private ViewPagerTabs mTabs;
	private TestPagerAdapter mAdapter;
	  
	@Override	
	public boolean onCreateOptionsMenu(Menu menu) {
	    				
		menu.add(0, MENU_FILTER, 0, getString(R.string.sliding_tabs_pager_social_action_bar_menu_filter))	
			.setIcon(R.drawable.ic_menu_moreoverflow)
			.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
		
		menu.add(0, MENU_SORT, 0, getString(R.string.sliding_tabs_pager_social_action_bar_menu_sort))
			.setIcon(R.drawable.ic_title_share_default)
			.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
			
		menu.add(0, MENU_SEARCH, 0, getString(R.string.sliding_tabs_pager_social_action_bar_menu_search))
	        .setIcon(R.drawable.ic_search)
	        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
		
		return super.onCreateOptionsMenu(menu);
	}
	
	 
	@Override
	    
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case android.R.id.home:
				// app icon in action bar clicked; go home
				Intent intent = new Intent(this, MainActivityV2.class);        
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
				return true;
				
			default:
                return super.onOptionsItemSelected(item);
		}

    }
		            
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.sliding_tabs_pager_social);
   
        getSupportActionBar().setTitle(getResources().getString(R.string.btn_social_main));
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        
        getSupportActionBar().setDisplayUseLogoEnabled(true);
        
        ArrayList<String> fragments = new ArrayList<String>();
        fragments.add(FriendsFragment.class.getName());
        fragments.add(FriendsFragment.class.getName());
        
        mAdapter = new TestPagerAdapter(this, getSupportFragmentManager(), fragments);
		
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mAdapter);
		
		mTabs = (ViewPagerTabs) findViewById(R.id.tabs);
		mTabs.setViewPager(mViewPager);
		
		mTabs.setTextColor(0xFF999999);
		mTabs.setTextColorCenter(0xFF999999);		
		mTabs.setLineHeight(0);
		
		if (savedInstanceState != null) {
        	mTabs.setViewPager(mViewPager, savedInstanceState.getInt("index"));       	
        }
	}
	
	public static class TestPagerAdapter extends FragmentStatePagerAdapter implements
    ViewPagerTabProvider {

		private String[] mTitles;
		private ArrayList<String> mFragments;
		private Context mContext;
		
		public TestPagerAdapter(Context context, FragmentManager fm, 
				ArrayList<String> fragments) {
		    super(fm);
		    mFragments = fragments;
		    mContext = context;	 
		    Resources res = mContext.getResources();
		    mTitles = res.getStringArray(R.array.sliding_tabs_pager_social_tabs);
		}

		@Override
		public Object instantiateItem(View container, int position) {
		    return Fragment.instantiate(mContext, mFragments.get(position), null);
		}
		
		@Override
		public Fragment getItem(int position) {
		    return Fragment.instantiate(mContext, mFragments.get(position), null);
		}
		
		@Override
		public int getCount() {
		    return mFragments.size();
		}
		
		@Override
		public String getTitle(int position) {
		    
		    return mTitles[position];
		}

    }
}
